﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.BLL;
using OA.Core;
using OA.Core.Helper;
using OA.Model;
using OA.Web.Code;
using OA.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace OA.Web.Areas.Admin.Controllers
{
    [Area("Admin")] 
    public class LoginController : BaseController
    {

        private IMemoryCache _cache;
        private ICookie _cookie;
        public LoginController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
            _cookie = cookie;
        }

        [HttpGet]
        public IActionResult Index()
        {
            return View(new LoginInfo());
        }

        [HttpPost]
        public IActionResult Index(LoginInfo info,string url)
        {
            if (string.IsNullOrWhiteSpace(info.UserName))
            {
                ViewBag.ErrorMsg = "请输入账号";
                return View(info);
            }
            if (string.IsNullOrWhiteSpace(info.Password))
            {
                ViewBag.ErrorMsg = "请输入密码";
                return View(info);
            }
            if (string.IsNullOrWhiteSpace(info.ValidateCode))
            {
                ViewBag.ErrorMsg = "请输入验证码";
                return View(info);
            }
            if (HttpContext.Session.GetString("CaptchaResult_1") != info.ValidateCode.ToLower())
            {
                HttpContext.Session.Remove("CaptchaResult_0");
                ViewBag.ErrorMsg = "请输入验证码";
                return View(info);
            }

            LoginLog infoLoginLog = new LoginLog();
            HttpContextAccessor context = new HttpContextAccessor();
            infoLoginLog.IP = context.HttpContext?.Connection.RemoteIpAddress.ToString();
            infoLoginLog.Platform = "PC";
            infoLoginLog.UserName = info.UserName;
            infoLoginLog.TypeID = 3;

            using (var dbContent = new MyDbContext())
            {
                AdminInfo infoAdmin = dbContent.AdminInfo.Where(m => m.UserName == info.UserName).FirstOrDefault();
                string mm = (info.UserName+info.Password).ToMd5String();
                if (infoAdmin == null || infoAdmin.PassWord != mm)
                {
                    ViewBag.ErrorMsg = "用户名或密码输入错误";
                    infoLoginLog.Status = -1;
                    this.AddInfo<LoginLog>(infoLoginLog);
                    return View(info);
                }
                if (infoAdmin.Status == 2)
                {
                    ViewBag.ErrorMsg = "账号异常！请联系管理员解除账号异常状态";
                    return View(info);
                }
                else
                {
                    infoLoginLog.UserID = infoAdmin.ID;
                    infoLoginLog.Status = 1;
                    this.AddInfo<LoginLog>(infoLoginLog);

                    _cookie.SetCookies(CookieHelper.AdminID, infoAdmin.UserName.ToBase62());
                    _cookie.SetCookies(CookieHelper.AdminName, HttpUtility.UrlEncode(infoAdmin.UserName, Encoding.UTF8));
                    if (string.IsNullOrWhiteSpace(url))
                    {
                        return Redirect("/Admin/Home/Index");
                    }
                    else
                    {
                        return Redirect(url);
                    }
                }
            }
        }

    }
}
